package com.chushouya.controller.product.admin;

import com.chushouya.common.constant.ServiceType;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.schema.PageResult;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.general.framework.web.annotations.RepeatSubmit;
import com.chushouya.product.dto.admin.brand.BrandDetailDTO;
import com.chushouya.product.dto.admin.brand.BrandListDTO;
import com.chushouya.product.dto.admin.brand.BrandQuery;
import com.chushouya.product.service.admin.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = ServiceType.PLATFORM + "品牌管理")
@RestController
@RequestMapping(ServiceType.PLATFORM + "/brand")
public class BrandController {

    @Resource
    private BrandService brandService;

    @ApiOperation("获取品牌列表")
    @GetMapping("/list")
    public Response<PageResult<BrandListDTO>> list(BrandQuery query) {
        return Response.success(brandService.selectBrandPageResult(query));
    }

    @ApiOperation("根据brandId获取详细信息")
    @GetMapping("/detail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brandId", value = "品牌id", required = true),
    })
    public Response<BrandDetailDTO> getDetail(Long brandId) {
        return Response.success(brandService.getBrandDetail(brandId));
    }

    @ApiOperation("新增品牌")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @RepeatSubmit
    public Response<Long> add(@Validated @RequestBody BrandDetailDTO brand) {
        return Response.success(brandService.addBrand(brand));
    }

    @ApiOperation("修改品牌")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Response<Long> edit(@Validated @RequestBody BrandDetailDTO brand) {
        return Response.success(brandService.editBrand(brand));
    }

    @ApiOperation("修改品牌状态")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/editStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brandId", value = "品牌id", required = true),
            @ApiImplicitParam(name = "status", value = "状态 0下架 1上架", required = true)
    })
    public Response<Long> editStatus(Long brandId, Integer status) {
        return Response.success(brandService.editBrandStatus(brandId, status));
    }

    @ApiOperation("删除品牌")
    @Log(businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brandId", value = "品牌id", required = true)
    })
    public Response<Void> remove(Long brandId) {
        brandService.removeBrand(brandId);
        return Response.success();
    }
}
